package org.jeecg.chase.modules.advert.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.chase.modules.advert.entity.AdhAdvert;
import org.jeecg.chase.modules.advert.service.IAdhAdvertService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 轮播广告管理
 * @Author: jeecg-boot
 * @Date:   2022-01-08
 * @Version: V1.0
 */
@Api(tags="轮播广告管理")
@RestController
@RequestMapping("/advert/adhAdvert")
@Slf4j
public class AdhAdvertController extends JeecgController<AdhAdvert, IAdhAdvertService> {
	@Autowired
	private IAdhAdvertService adhAdvertService;
	
	/**
	 * 分页列表查询
	 *
	 * @param adhAdvert
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-分页列表查询")
	@ApiOperation(value="轮播广告管理-分页列表查询", notes="轮播广告管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AdhAdvert adhAdvert,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AdhAdvert> queryWrapper = QueryGenerator.initQueryWrapper(adhAdvert, req.getParameterMap());
		queryWrapper.orderByAsc("adh_advert_sort");
		Page<AdhAdvert> page = new Page<AdhAdvert>(pageNo, pageSize);
		IPage<AdhAdvert> pageList = adhAdvertService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param adhAdvert
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-添加")
	@ApiOperation(value="轮播广告管理-添加", notes="轮播广告管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AdhAdvert adhAdvert) {
		adhAdvertService.save(adhAdvert);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param adhAdvert
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-编辑")
	@ApiOperation(value="轮播广告管理-编辑", notes="轮播广告管理-编辑")
	@PostMapping(value = "/edit")
	public Result<?> edit(@RequestBody AdhAdvert adhAdvert) {
		adhAdvertService.updateById(adhAdvert);
		return Result.OK("编辑成功!");
	}

	 @AutoLog(value = "轮播广告管理-切换状态")
	 @ApiOperation(value="轮播广告管理-切换状态", notes="轮播广告管理-切换状态")
	 @PostMapping(value = "/changeStatus")
	 public Result<?> changeStatus(@RequestBody AdhAdvert adhAdvert) {
		  adhAdvert = adhAdvertService.getById(adhAdvert.getId());
		 if("1".equals(adhAdvert.getAdhAdvertStatus())){
			 adhAdvert.setAdhAdvertStatus("2");
		 }else{
			 adhAdvert.setAdhAdvertStatus("1");
		 }
		 adhAdvertService.updateById(adhAdvert);
		 return Result.OK("切换状态成功!");
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-通过id删除")
	@ApiOperation(value="轮播广告管理-通过id删除", notes="轮播广告管理-通过id删除")
	@GetMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		adhAdvertService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-批量删除")
	@ApiOperation(value="轮播广告管理-批量删除", notes="轮播广告管理-批量删除")
	@GetMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.adhAdvertService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "轮播广告管理-通过id查询")
	@ApiOperation(value="轮播广告管理-通过id查询", notes="轮播广告管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AdhAdvert adhAdvert = adhAdvertService.getById(id);
		if(adhAdvert==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(adhAdvert);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param adhAdvert
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AdhAdvert adhAdvert) {
        return super.exportXls(request, adhAdvert, AdhAdvert.class, "轮播广告管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AdhAdvert.class);
    }

    @RequestMapping(value = "/getAllAdhAdvert", method = RequestMethod.GET)
    public Result<?> getAllAdhAdvert(){
		QueryWrapper<AdhAdvert> queryWrapper =  new QueryWrapper<AdhAdvert>();
		queryWrapper.eq("adh_advert_status","1");
		queryWrapper.orderByAsc("adh_advert_sort");
		List<AdhAdvert> list = adhAdvertService.list(queryWrapper);
		return Result.OK(list);
	}
}
